<?php
namespace Home\Controller;
use Admin\Service\BaseService;
use Model\UserModel;
use Think\Controller;

class MemberController extends BaseController {

    public function __construct()
    {
        parent::__construct();

    }

    public function index()
    {

        $this->display();

    }

    public function saveMessage(){

        $userModel = new UserModel();
        $data = $userModel->create();
        $oldPhone = I("post.oldPhone");
        $code = I("post.code");
        $data['id'] = cookie("id");
        $image = $_FILES['image'];

        $id   = intval($this->request->post('id'));
        $type =1;
        if(!empty($oldPhone)){
            $type =2;
        }
        if(!empty($image['name'])){
            $baseService = new BaseService();
            $image = $baseService->upload($image);
            $data['picture'] = $image;
            $type =3;
        }


        switch($type){
            case 1:
                $saveMessage = $userModel->save($data);
                if($saveMessage){
                    $this->success("修改成功","/Member");
                }else{
                    $this->error("修改失败1","/Member");
                }
                break;
            case 2:
                $findMessage = $userModel->where("`phone` = '{$oldPhone}'")->find();
                if(empty($findMessage)){
                    $this->error("旧手机号填写错误","/Member");

                }else{
                    if($code !=="123456"){
                        $this->error("验证码错误","/Member");
                    }
                    $saveMessage = $userModel->save($data);
                    if($saveMessage){
                        $this->success("修改成功","/Member");
                    }else{
                        $this->error("修改失败2","/Member");
                    }
                }
                break;
            case 3:

                $saveMessage = $userModel->save($data);
                if($saveMessage){
                    $this->success("修改成功","/Member");
                }else{
                    $this->error("修改失败","/Member");
                }
                break;
        }

    }


}